import { ClockInResult, ClockOutResult } from './components/ImportData/parseAttendanceFileToModel/AttendanceRecord';
import { AttendanceRecordItemModel, attendanceRecordModel, officialHolidays } from './components/ImportData/parseAttendanceFileToModel/store';
import xlsx from 'xlsx';
import { saveAs } from 'file-saver';
import Department from './components/ImportData/parseAttendanceFileToModel/Department';
import { Moment } from 'moment';
import { message } from 'antd';

export function exportData(model: Map<Department, AttendanceRecordItemModel[]>) {
    const { startDate, endDate } = attendanceRecordModel;
    if (startDate !== null && endDate !== null) {
        const workBook = xlsx.utils.book_new();
        model.forEach((attendanceRecordItemModelList, department) => {
            const sheet = createSheet(attendanceRecordItemModelList, startDate, endDate);
            xlsx.utils.book_append_sheet(workBook, sheet, department.name);
        });
        const wbout = xlsx.write(workBook, { bookType: 'xlsx', bookSST: false, type: 'array' });
        saveAs(new Blob([wbout], { type: 'application/octet-stream' }), '考勤汇总.xlsx');
    } else {
        message.warning('数据的起始日期和结束日期为空');
    }
}

export function createSheet(attendanceRecordItemModelList: AttendanceRecordItemModel[], startDate: Moment, endDate: Moment) {
    const data: string[][] = [
        [
            '序号',
            '考勤号',
            '姓名',
            '迟到10分钟以内（次）',
            '迟到10分钟以上（次）',
            '无打卡天数（次）',
            '出勤（天）',
            '应出勤（天）',
            '公休（天）',
            '超假（天）'
        ]
    ];
    let i = 1;
    attendanceRecordItemModelList.forEach(({ worker, attendanceRecordList }) => {
        let lateTenMinutesTime = 0;
        let lateMoreThanTenMinutesTime = 0;
        let notClockedDays = 0;
        let attendanceDay = 0;
        attendanceRecordList.forEach(record => {
            switch (record.clockInfo.clockInInfo.clockInResult) {
                case ClockInResult.NORMAL:
                    attendanceDay += 0.5;
                    break;
                case ClockInResult.BE_LATE:
                    attendanceDay += 0.5;
                    lateTenMinutesTime++;
                    break;
                case ClockInResult.TOO_LATE:
                    attendanceDay += 0.5;
                    lateMoreThanTenMinutesTime++;
                    break;
                case ClockInResult.ABSENTEEISM_LATE:
                    notClockedDays += 0.5;
                    break;
            }

            switch (record.clockInfo.clockOutInfo.clockOutResult) {
                case ClockOutResult.NORMAL:
                    attendanceDay += 0.5;
                    break;
                case ClockOutResult.EARLY:
                    attendanceDay += 0.5;
                    break;
                case ClockOutResult.ABSENTEEISM_EARLY:
                    notClockedDays += 0.5;
                    break;
            }
        });
        let beyondTheHolidays = notClockedDays - officialHolidays;
        if (beyondTheHolidays < 0) {
            beyondTheHolidays = 0;
        }
        let totalDays = 0;
        if (endDate !== null && startDate != null) {
            totalDays = endDate.diff(startDate, 'days') + 1;
        }
        const item = [
            String(i++),
            worker.id,
            worker.name,
            String(lateTenMinutesTime || ''),
            String(lateMoreThanTenMinutesTime || ''),
            String(notClockedDays || ''),
            String(attendanceDay || ''),
            String(totalDays - officialHolidays || ''),
            String(officialHolidays || ''),
            String(beyondTheHolidays || '')
        ];
        data.push(item);
    });
    return xlsx.utils.aoa_to_sheet(data);
}